Welcome back, for a little way of the programmer segment on how to decode a function.
Here's a habit that I hope you'll cultivate whenever
you see a function definition or whenever you write one.
Decode that function definition,
ask yourself three questions;
First, how many parameters?
Second, what types and values will be bound to those parameter names?
Third, what is the type of the return value?
So, let's work through a few examples.
Here's a function cyu3,
how many parameters does it have?
That's our first question whenever we try to decode a function,
and it's the easiest one to answer,
cause you can just look inside
the parentheses and you can see that there are three variable names separated by commas.
So, three inputs, and that's exactly the question that's being asked.
Sure? Three inputs x, y, and z.
The next question is,
what will their types be?
So, what are the types of x, y, and z?
The first question is asking you is for x and y,
cause it turns out they have to have the same type.
How can you tell that?
There's nothing in the function declaration that tells you that.
So, you have to look in the code and see how those variables are used.
In particular, we have x minus y in this expression.
What are the types of objects that you can do a minus on?
The answer is numbers.
They could be integers, they could be floats,
strings not so much, and not this either.
Let's try that, integers and floats.
Sure enough, we are correct.
How about the type of z?
Well, we have to look where z is used,
and z is used down here in the else.
We're running an append method on it.
Which objects can you do append on?
The answer is only lists.
Remember that lists are mutable.
You can do append, strings you're not allowed to.
So, C is correct.
If I had answered D, I get a little feedback telling me,
"Append can't be performed on strings."
Then the final question for decoding of a function is,
what kind of return value does it give you?
There are two spots in this code,
where we're returning a value either y minus 2 or x plus 3.
We previously inferred that both y and x had to be numbers,
either integers or floats,
and this doesn't tell us anymore about whether they're going to be integers or floats.
Minus and plus are both operations that work on both of them,
so the return value could either be an integer or a float.
Your debugging sessions will be a lot shorter,
if you can always answer those questions about any function that you're working with.
So, build the habit.
Whenever you see a function decoded by figuring out,
how many input parameters?
What are their types? What value will be returned? See you next time.